package com.sd.util;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.alex.books.BaseBook;

public class DownloaderEngine {
	private BaseAdapter mBookAdapter;
	private Context mContext;
	public Hashtable<BaseBook, DownloadChannel> mDownChannels;
	
	private static DownloaderEngine instance;
	
	public static DownloaderEngine getInstance(){
		if(instance == null){
			instance = new DownloaderEngine();
		}
		return instance;
	}
	
	public DownloaderEngine(){
		mDownChannels = new Hashtable<BaseBook, DownloadChannel>();
	}
	
	public void setBookAdapter(BaseAdapter adapter){
		mBookAdapter = adapter;
	}
	
	public BaseAdapter getAdapter(){
		return mBookAdapter;
	}
	
	public void setContext(Context context){
		mContext = context;
	}
	
	public DownloadChannel getDownloadChannel(BaseBook baseBook){
		DownloadChannel downchannel = null;
		for (Iterator it = mDownChannels.entrySet().iterator(); it.hasNext(); ) 
		{ 
			Map.Entry entry = (Map.Entry) it.next(); 
			if(baseBook.mBookId == ((BaseBook)entry.getKey()).mBookId){
				downchannel = (DownloadChannel)entry.getValue();
			}
		}  
		return downchannel;
	}
	
	
	public void addDownloadChannel(BaseBook baseBook,RelativeLayout relative){
		DownloadChannel downchannel = new DownloadChannel(baseBook,relative,mContext);
		mDownChannels.put(baseBook, downchannel);
	}
	
}
